import React, {PureComponent} from 'react';
import {connect} from 'dva';
import {
    Form, Input, Button, Card, Select,DatePicker, TimePicker
} from 'antd';
import {message} from "antd/lib/index";
import moment from "moment";

const FormItem = Form.Item;
moment().format('YYYY-MM-DD HH:mm:ss');
@connect(state => ({
    submitting: state.orderManage.loading,
}))
@Form.create()
export default class OrderEdit extends PureComponent {




    handleParentModalVisible = (e) => {
        this.props.handleModalVisible();
    }



    handleSubmit = (editType,order_id,e) => {


        this.props.form.validateFieldsAndScroll((err, values) => {
            if (!err) {
                let user_id = '';
                const userInfoState = sessionStorage.getItem("userInfoState");
                if (userInfoState !== null) {
                    const {userInfo} = JSON.parse(userInfoState);
                    user_id = userInfo.user_id;
                }


                this.props.dispatch({
                    type: `orderManage/${editType}`,
                    payload: {...values,order_id: order_id,user_id:user_id},
                    callback: (response) => {
                        if (response.result === 0) {
                            message.success('提交成功');
                            this.props.handleModalVisible(false);
                            this.props.handleModalSuccess(this.props.pagination);
                        } else {
                            message.success('提交失败');
                        }
                    }
                });
                console.log(values);
            }
        });
    }

    render() {
        const {submitting, record, editType, typeSList} = this.props;
        const {getFieldDecorator, getFieldValue} = this.props.form;

        const formInitValue = editType === 'add' ? '' : record;
        const order_id = editType === 'add' ? '' : record.order_id;
        const formItemLayout = {
            labelCol: {
                xs: {span: 24},
                sm: {span: 7},
            },
            wrapperCol: {
                xs: {span: 24},
                sm: {span: 12},
                md: {span: 10},
            },
        };
        const submitFormLayout = {
            wrapperCol: {
                xs: {span: 24, offset: 0},
                sm: {span: 10, offset: 7},
            },
        };
        const config = {
            rules: [{ type: 'object', required: true, message: 'Please select time!' }],
        };

        return (
            <Card bordered={false}>
                <Form
                    onSubmit={this.handleSubmit }
                    hideRequiredMark
                >
                    {/*<FormItem*/}
                        {/*{...formItemLayout}*/}
                        {/*label="会员ID"*/}
                    {/*>*/}
                        {/*{getFieldDecorator('user_id', {*/}
                            {/*initialValue: formInitValue.user_id || '',*/}
                            {/*rules: [{*/}
                                {/*required: true, message: '请输入会员ID',*/}
                            {/*}],*/}
                        {/*})(*/}
                            {/*<Input placeholder="请输入会员ID"/>*/}
                        {/*)}*/}
                    {/*</FormItem>*/}
                    <FormItem
                        {...formItemLayout}
                        label="配送方式"
                    >
                        {getFieldDecorator('shipping_id', {
                            initialValue: formInitValue !== '' ? ((formInitValue.shipping_id ? "1" : "0") || '') :'',
                        })(
                            <Select placeholder="请选择" style={{width: '100%'}} >
                                <Option value="">请选择</Option>
                                <Option value="1">京东</Option>
                                <Option value="2">顺丰</Option>
                                <Option value="3">韵达</Option>
                            </Select>
                        )}
                    </FormItem>
                    <FormItem
                        {...formItemLayout}
                        label="支付方式"
                    >
                        {getFieldDecorator('pay_id', {
                            initialValue: formInitValue !== '' ? ((formInitValue.pay_id ? "1" : "0") || '') :'',
                        })(
                            <Select placeholder="请选择" style={{width: '100%'}} >
                                <Option value="">请选择</Option>
                                <Option value="1">支付宝</Option>
                                <Option value="2">微信</Option>
                            </Select>
                        )}
                    </FormItem>
                    {/*<FormItem*/}
                        {/*{...formItemLayout}*/}
                        {/*label="订单号"*/}
                    {/*>*/}
                        {/*{getFieldDecorator('order_sn', {*/}
                            {/*initialValue: formInitValue.order_sn || '',*/}
                            {/*rules: [{*/}
                                {/*required: true, message: '请输入订单号',*/}
                            {/*}],*/}
                        {/*})(*/}
                            {/*<Input placeholder="请输入订单号"/>*/}
                        {/*)}*/}
                    {/*</FormItem>*/}
                    {/*<FormItem*/}
                        {/*{...formItemLayout}*/}
                        {/*label="订单状态"*/}
                    {/*>*/}
                        {/*{getFieldDecorator('order_status', {*/}
                            {/*initialValue: formInitValue !== '' ? ((formInitValue.order_status ? "1" : "0") || '') :'',*/}
                        {/*})(*/}
                            {/*<Select placeholder="请选择" style={{width: '100%'}} >*/}
                                {/*<Option value="">请选择</Option>*/}
                                {/*<Option value="0">未确认</Option>*/}
                                {/*<Option value="1">确认</Option>*/}
                                {/*<Option value="2">已取消</Option>*/}
                                {/*<Option value="3">无效</Option>*/}
                                {/*<Option value="4">退货</Option>*/}

                            {/*</Select>*/}
                        {/*)}*/}
                    {/*</FormItem>*/}
                    {/*<FormItem*/}
                        {/*{...formItemLayout}*/}
                        {/*label="配送情况"*/}
                    {/*>*/}
                        {/*{getFieldDecorator('shipping_status', {*/}
                            {/*initialValue: formInitValue !== '' ? ((formInitValue.shipping_status ? "1" : "0") || '') :'',*/}
                        {/*})(*/}
                            {/*<Select placeholder="请选择" style={{width: '100%'}} >*/}
                                {/*<Option value="">请选择</Option>*/}
                                {/*<Option value="0">未发货</Option>*/}
                                {/*<Option value="1">已发货</Option>*/}
                                {/*<Option value="2">已收货</Option>*/}
                                {/*<Option value="3">退货</Option>*/}
                            {/*</Select>*/}
                        {/*)}*/}
                    {/*</FormItem>*/}
                    {/*<FormItem*/}
                        {/*{...formItemLayout}*/}
                        {/*label="支付状态"*/}
                    {/*>*/}
                            {/*{getFieldDecorator('pay_status', {*/}
                            {/*initialValue: formInitValue !== '' ? ((formInitValue.pay_status ? "1" : "0") || '') :'',*/}
                        {/*})(*/}
                            {/*<Select placeholder="请选择" style={{width: '100%'}} >*/}
                                {/*<Option value="">请选择</Option>*/}
                                {/*<Option value="0">未付款</Option>*/}
                                {/*<Option value="1">付款中</Option>*/}
                                {/*<Option value="2">已付款</Option>*/}
                            {/*</Select>*/}
                        {/*)}*/}
                    {/*</FormItem>*/}
                    <FormItem
                        {...formItemLayout}
                        label="订单附言"
                    >
                        {getFieldDecorator('postscript', {
                            initialValue: formInitValue.postscript || '',
                            rules: [{
                                required: true, message: '请输入订单附言',
                            }],
                        })(
                            <Input placeholder="请输入订单附言"/>
                        )}
                    </FormItem>
                    <FormItem
                        {...formItemLayout}
                        label="订单金额"
                    >
                        {getFieldDecorator('goods_amount', {
                            initialValue: formInitValue.goods_amount || '',
                            rules: [{
                                required: true, message: '请输入订单金额',
                            }],
                        })(
                            <Input placeholder="请输入订单金额"/>
                        )}
                    </FormItem>
                    <FormItem
                        {...formItemLayout}
                        label="配送费用"
                    >
                        {getFieldDecorator('shipping_fee', {
                            initialValue: formInitValue.shipping_fee || '',
                            rules: [{
                                required: true, message: '请输入配送金额',
                            }],
                        })(
                            <Input placeholder="请输入配送费用"/>
                        )}
                    </FormItem>
                    <FormItem
                        {...formItemLayout}
                        label="已付金额"
                    >
                        {getFieldDecorator('money_paid', {
                            initialValue: formInitValue.money_paid || '',
                            rules: [{
                                required: true, message: '请输入已付金额',
                            }],
                        })(
                            <Input placeholder="请输入已付金额"/>
                        )}
                    </FormItem>
                    <FormItem
                        {...formItemLayout}
                        label="使用的积分数量"
                    >
                        {getFieldDecorator('integral', {
                            initialValue: formInitValue.integral || '',
                            rules: [{
                                required: true, message: '使用的积分数量',
                            }],
                        })(
                            <Input placeholder="请输入使用的积分数量"/>
                        )}
                    </FormItem>
                    <FormItem
                        {...formItemLayout}
                        label="使用积分金额"
                    >
                        {getFieldDecorator('integral_money', {
                            initialValue: formInitValue.integral_money || '',
                            rules: [{
                                required: true, message: '请输入使用积分金额',
                            }],
                        })(
                            <Input placeholder="请输入使用积分金额"/>
                        )}
                    </FormItem>
                    <FormItem
                        {...formItemLayout}
                        label="使用红包金额"
                    >
                        {getFieldDecorator('bonus', {
                            initialValue: formInitValue.bonus || '',
                            rules: [{
                                required: true, message: '请输入使用红包金额',
                            }],
                        })(
                            <Input placeholder="请输入使用红包金额"/>
                        )}
                    </FormItem>

                    <FormItem
                        {...formItemLayout}
                        label="应付款金额"
                    >
                        {getFieldDecorator('order_amount', {
                            initialValue: formInitValue.order_amount || '',
                            rules: [{
                                required: true, message: '请输入应付款金额',
                            }],
                        })(
                            <Input placeholder="请输入应付款金额"/>
                        )}
                    </FormItem>
                    {/*<FormItem*/}
                        {/*{...formItemLayout}*/}
                        {/*label="订单生成时间"*/}
                    {/*>*/}
                        {/*{getFieldDecorator('add_time', config)(*/}
                            {/*<DatePicker showTime format="YYYY-MM-DD HH:mm:ss"  onChange={this.onChange} />*/}
                        {/*)}*/}
                    {/*</FormItem>*/}
                    {/*<FormItem*/}
                        {/*{...formItemLayout}*/}
                        {/*label="订单确认时间"*/}
                    {/*>*/}
                        {/*{getFieldDecorator('confirm_time', config)(*/}
                            {/*<DatePicker format="YYYY-MM-DD HH:mm:ss" />*/}
                        {/*)}*/}
                    {/*</FormItem>*/}
                    {/*<FormItem*/}
                        {/*{...formItemLayout}*/}
                        {/*label="订单支付时间"*/}
                    {/*>*/}
                        {/*{getFieldDecorator('pay_time', config)(*/}
                            {/*<DatePicker format="YYYY-MM-DD HH:mm:ss" />*/}
                        {/*)}*/}
                    {/*</FormItem>*/}
                    {/*<FormItem*/}
                        {/*{...formItemLayout}*/}
                        {/*label="订单配送时间"*/}
                    {/*>*/}
                        {/*{getFieldDecorator('shipping_time', config)(*/}
                            {/*<DatePicker  format="YYYY-MM-DD HH:mm:ss" />*/}
                        {/*)}*/}
                    {/*</FormItem>*/}






                    <FormItem {...submitFormLayout} style={{marginTop: 32}}>
                        <Button type="primary" loading={submitting} onClick={this.handleSubmit.bind(this,editType,order_id)}>
                            提交
                        </Button>
                        <Button style={{marginLeft: 8}} onClick={this.handleParentModalVisible}>取消</Button>
                    </FormItem>
                </Form>
            </Card>
        );
    }
}
